<?php
define('JXW_DEBUG', 'data/debug/');
define('JXW_LOG', 'data/volam.log.txt');

function breakpoint() {
  ob_flush();
  flush();
  sleep(0.1);
  debugBreak();
}

function addDebug($obj) {
  return;
  $filename = JXW_DEBUG . date('Y-m-d-H-i-s-') . rand(100000, 999999);
  file_put_contents($filename, serialize($obj));
}

function addLog($message, $backlevel = 0) {
  global $JXWA;
  if (! is_string($message)) $message = serialize($message);
  $prefix = $JXWA->user ? '('. $JXWA->config['UserInfo']['Name'] .','. $JXWA->user['server'] . ') ' : '';
  $bt = debug_backtrace();
  for ($k=$backlevel; $k<count($bt); $k++)
    $message .= ' '. basename($bt[$k]['file'], '.php') .':'. $bt[$k]['line'];
  file_put_contents(JXW_LOG, date('d/m H:i:s '). $prefix . $message ."\n", FILE_APPEND);
}

function viewLog() {
  global $JXWA;
  echo '<ul>';
  $regex = func_get_args();
  $uname = '('.$JXWA->config['UserInfo']['Name'];
  $handle = @fopen(JXW_LOG, 'r');
  if ($handle) {
    while (!feof($handle)) {
      $buffer = fgets($handle, 1024);
      if (strpos($buffer, $uname) === false) continue;
      $cond = true;
      foreach ($regex as $pat) {
        if (strpos($buffer, $pat) === false) {
          $cond = false;
          break;
        }
      }
      if ($cond) echo '<li>'.str_replace('<', '&lt;', $buffer).'</li>';
    }
    fclose($handle);
  }
  echo '</ul>';
}

function printObj($obj) {
  echo '<xmp>';
  var_dump($obj);
  echo '</xmp>';
}

class Tools {

  /**
   * Convert (x,y) into scalar-value position
   */
  static function XyToPos($x, $y) {
    return ($y-1)*400 + $x;
  }

  /**
   * Convert scalar-value position into (x,y)
   */
  static function PosToXy($Pos) {
    $x = ($Pos - 1) % 400 + 1;
    $y = round(($Pos - $x) / 400) + 1;
    return empty($Pos) ? array() : array($x, $y);
  }

  /**
   * Calculate the base travel time between two positions
   */
  static function TravelTime($JXWA, $Pos) {
    list($x1, $y1) = self::PosToXy($JXWA->CityPos);
    list($x2, $y2) = self::PosToXy($Pos);
    return intval(round(18*sqrt(pow($x1-$x2, 2) + pow($y1-$y2, 2)))*5 + 210);
  }

  /**
   * JSON encoder
   */
  static function encode($message) {
    $json = new Services_JSON();
    return $json->encode($message);
  }

  /**
   * JSON decoder
   */
  static function decode($message) {
    $json = new Services_JSON();
    if (substr($message, -3) == ';/*') {
      $message = substr($message, 0, -3);
    }
    return $json->decode($message);
  }

  static function arrayExport($obj, $keys) {
    $ret = array();
    foreach ($keys as $key=>$val) {
      if (is_int($key)) {
        $key = $val;
      }
      $ret[$val] = $obj->$key;
    }
    return $ret;
  }
  
  function reload($msg = '') {
    $location = $_SERVER['PHP_SELF'];
    if ($_GET['account']) $location .= '?account=' . $_GET['account'];
    
    if ($msg) {
      echo '<script>location="'. $location .'";</script>';
      echo $msg;
    }
    else {
      header('Location: '. $location);
    }
    die;
  }
}
